package com.shop.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.constant.MessageConstant;
import com.shop.constant.RedisConstant;
import com.shop.entity.QueryPageBean;
import com.shop.entity.Result;
import com.shop.pojo.Goods;
import com.shop.service.GoodsService;
import com.shop.utils.MinioUtils;
import com.shop.utils.UploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/goods")
@Api(tags = "商品管理")
public class GoodsController {
    @Reference
    private GoodsService goodsService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/add")
    @ApiOperation("添加商品接口")
    public Result add(@RequestBody Goods goods){
        if (goodsService.findGoodsName(goods.getName())!=null){
            return new Result(false,MessageConstant.ADD_GOODS_FAIL);
        }else{
            goodsService.add(goods);
            return new Result(true,MessageConstant.ADD_GOODS_SUCCESS);
        }
    }

    @PostMapping("/pageQuery")
    @ApiOperation("商品分页接口")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        Page<Goods> page=goodsService.pageQuery(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        return new Result(true,MessageConstant.QUERY_GOODS_SUCCESS,page);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除商品接口")
    @ApiImplicitParams({@ApiImplicitParam(dataType = "int",name = "id",value = "商品id",required = true,example = "0")})
    public Result delete(Integer id){
        try {
            goodsService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_GOODS_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_GOODS_SUCCESS);
    }

    @PutMapping("/edit")
    @ApiOperation("修改商品接口")
    public Result edit(@RequestBody Goods goods){
//        try {
//            goodsService.edit(goods);
//        }catch (Exception e){
//            e.printStackTrace();
//            return new Result(false,MessageConstant.EDIT_GOODS_FAIL);
//        }
//        return new Result(true,MessageConstant.EDIT_GOODS_SUCCESS);
        Goods g = goodsService.findById(goods.getId());
        if ((goodsService.findGoodsName(goods.getName())!=null) && !g.getName().equals(goods.getName())){
            return new Result(false,MessageConstant.EDIT_GOODS_FAIL);
        }else{
            goodsService.edit(goods);
            return new Result(true,MessageConstant.EDIT_GOODS_SUCCESS);
        }
    }

    @GetMapping("/findById")
    @ApiOperation("根据商品id查找商品接口")
    public Result findById(@RequestParam(required = true) Integer id){
        try {
            Goods goods=goodsService.findById(id);
            return new Result(true,MessageConstant.QUERY_GOODS_SUCCESS,goods);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_GOODS_FAIL);
        }
    }

    @GetMapping("/findAll")
    @ApiOperation("显示商品下拉列表接口")
    public Result findAll(){
        try {
            List<Goods> list=goodsService.findAll();
            return new Result(true,MessageConstant.QUERY_GOODS_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_GOODS_FAIL);
        }
    }

    @RequestMapping("/upload")
    @ApiOperation("商品图片上传接口")
    @ApiImplicitParams({@ApiImplicitParam(dataType = "MultipartFile",name = "imgFile",value = "图片名称",required = true,example = "1.jpg")})
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) throws NoSuchAlgorithmException, InvalidKeyException{
        System.out.println(imgFile);
        String originalFilename=imgFile.getOriginalFilename();
        int index=originalFilename.lastIndexOf(".");
        String extention=originalFilename.substring(index);
        String fileName= UUID.randomUUID().toString()+extention;
        UploadUtils uploadUtils=new UploadUtils();
        try {
            uploadUtils.add(imgFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            MinioUtils.uploadFile(fileName,originalFilename);
            redisTemplate.boundSetOps(RedisConstant.SETMEAL_PIC_RESOURCES).add(fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
    }
}
